package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    /**
     * 分页查询商品库存信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param codeOrName 商品编码或名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    @Override
    public Map<String, Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        HashMap<String, Object> result = new HashMap<>();
        int start = (page - 1) * rows;
        List<Goods> list = goodsDao.listInventory(start, rows, codeOrName, goodsTypeId);
        Integer count = goodsDao.selectCount();
        result.put("total", count);
        result.put("rows", list);
        return result;
    }

    /**
     * 分页查询商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param goodsName 商品名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    @Override
    public Map<String, Object> getGoodsListPage(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        HashMap<String, Object> result = new HashMap<>();
        int start = (page - 1) * rows;
        List<Goods> list = goodsDao.selectGoodsListPage(start, rows, goodsName, goodsTypeId);
        Integer count = goodsDao.selectCount();
        result.put("total", count);
        result.put("rows", list);
        return result;
    }

    /**
     * 新增或修改商品信息
     * @param goodsId
     * @param goods
     */
    @Override
    public void saveOrUpdate(Integer goodsId, Goods goods) {
        if (goodsId == null){
            goodsDao.insert(goods);
        }else {
            goodsDao.update(goodsId, goods);
        }
    }

    /**
     * 删除商品信息
     * @param goodsId
     */
    @Override
    public void delete(Integer goodsId) {
        goodsDao.delete(goodsId);
    }

    /**
     * 分页查询无库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        HashMap<String, Object> result = new HashMap<>();
        int start = (page - 1) * rows;
        List<Goods> list = goodsDao.selectNoInventoryQuantityPage(start, rows, nameOrCode);
        Integer count = goodsDao.selectNoInventoryQuantityCount();
        result.put("total", count);
        result.put("rows", list);
        return result;
    }

    /**
     * 分页查询有库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        HashMap<String, Object> result = new HashMap<>();
        int start = (page - 1) * rows;
        List<Goods> list = goodsDao.selectHasInventoryQuantityPage(start, rows, nameOrCode);
        Integer count = goodsDao.selectHasInventoryQuantityCount();
        result.put("total", count);
        result.put("rows", list);
        return result;
    }

    /**
     * 添加商品期初库存
     * @param goodsId 商品ID
     * @param inventoryQuantity 库存
     * @param purchasingPrice 成本价
     * @return
     */
    @Override
    public void saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        goodsDao.saveStock(goodsId, inventoryQuantity, purchasingPrice);
    }

    /**
     * 删除商品库存
     * @param goodsId 商品ID
     * @return
     */
    @Override
    public void deleteStock(Integer goodsId) {
        goodsDao.deleteStock(goodsId);
    }

    /**
     * 报损减库存
     * @param goodsId 商品ID
     * @param goodsNum 商品扣减库存
     */
    @Override
    public void subtractStock(Integer goodsId, Integer goodsNum) {
        goodsDao.subtractStock(goodsId, goodsNum);
    }

    /**
     * 报溢减库存
     * @param goodsId 商品ID
     * @param goodsNum 商品扣减库存
     */
    @Override
    public void addStock(Integer goodsId, Integer goodsNum) {
        goodsDao.addStock(goodsId, goodsNum);
    }

    /**
     * 查询库存报警商品信息
     * @return
     */
    @Override
    public Map<String, Object> listAlarm() {
        HashMap<String, Object> result = new HashMap<>();
        List<Goods> list = goodsDao.listAlarm();
        result.put("rows", list);
        return result;
    }

    /**
     * 添加或扣减库存
     * @param goodsId 商品id
     * @param goodsNum 修改的数量
     * @param price 成本
     * @param i 0为加库，1为扣库存
     */
    @Override
    public void updateGoodsStock(Integer goodsId, Integer goodsNum, double price, Integer state, int i) {
        if (i == 0){
            goodsDao.addStock(goodsId, goodsNum);
            goodsDao.updateLastPurchasingPrice(goodsId, price,state);
        }else if(i == 1) {
            goodsDao.subtractStock(goodsId, goodsNum);
            goodsDao.updateLastPurchasingPrice(goodsId, price,state);
        }
    }

    /**
     * 修改商品状态
     * @param goodsId 商品ID
     * @param state 0表示初始值,1表示已入库，2表示有进货或销售单据
     */
    @Override
    public void updateGoodsState(Integer goodsId, int state) {
        goodsDao.updateGoodsState(goodsId, state);
    }


}
